package lab02.test24;

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        int sum = 0;

        String date2;
        String date1;

        String maxDate;
        String minDate;
        Scanner sc = new Scanner(System.in);
        GetDays getDays = new GetDays();


        /*输入两个日期*/
        System.out.println("请输入第一个日期：\n比如输入20100101");
        date1 = sc.nextLine();

        System.out.println("请输入第二个日期：\n比如输入20100101");
        date2 = sc.nextLine();



        /*start-比较两个日期的先后顺序*/
        if(Integer.parseInt(date1.substring(0, 4))>Integer.parseInt(date2.substring(0, 4))){
            maxDate = date1;
            minDate = date2;
        }else if(Integer.parseInt(date1.substring(0, 4))<Integer.parseInt(date2.substring(0, 4))){
            maxDate = date2;
            minDate = date1;
        }else{
            //当年份相等时继续判断月份

            if(Integer.parseInt(date1.substring(4, 6))>Integer.parseInt(date2.substring(4, 6))){
                maxDate = date1;
                minDate = date2;

            }else if (Integer.parseInt(date1.substring(4, 6))<Integer.parseInt(date2.substring(4, 6))){
                maxDate = date2;
                minDate = date1;
            }else{
                //当月份相等时继续判断day
                if (Integer.parseInt(date1.substring(6, 8))>Integer.parseInt(date2.substring(6, 8))){
                    maxDate = date1;
                    minDate = date2;
                }else if (Integer.parseInt(date1.substring(6, 8))<Integer.parseInt(date2.substring(6, 8))){
                    maxDate = date2;
                    minDate = date1;
                }else {
                    //当年月日均相等时两个日期相等
                    maxDate = date1;
                    minDate = maxDate;
                }
            }
        }
        /*end-比较两个日期的先后顺序*/

        /*得到两个日期的年月日*/
        int maxDateYear = Integer.parseInt(maxDate.substring(0, 4));
        int maxDateMounth = Integer.parseInt(maxDate.substring(4, 6));
        int maxDateDay = Integer.parseInt(maxDate.substring(6, 8));

        int minDateYear = Integer.parseInt(minDate.substring(0, 4));
        int minDateMounth = Integer.parseInt(minDate.substring(4, 6));
        int minDateDay = Integer.parseInt(minDate.substring(6, 8));


        /*统计两年之间的年份总天数，不包含输入日期的两年*/
        for (int i = (minDateYear+1); i < maxDateYear; i++) {

            System.out.println("计算"+i+"年的天数！");

            sum+=365;

            if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0){
                sum +=1;
            }

        }


        /*加上maxDate的天数*/
        for (int j = 1; j <maxDateMounth ; j++) {

            System.out.println("加上maxDate的天数:"+"计算"+maxDateYear+"年"+j+"月的天数！");
            sum+=getDays.getDays(j,maxDateYear);
        }
        sum+=maxDateDay;

        /*加上minDate的天数*/
        for (int k = minDateMounth; k <=12; k++) {
            System.out.println("加上minDate的天数"+"计算"+minDateYear+"年"+k+"月的天数！");
            sum+=getDays.getDays(k,minDateYear);
        }
        sum-=minDateDay;

        /*如果两个日期在同一年，该程序会多加该年的天数*/
        /*减去该年天数*/
       if(maxDateYear == minDateYear){
           for (int m = 1; m <=12 ; m++) {
               sum-= getDays.getDays(m,minDateYear);
           }
       }

        /*两个日期间隔天数计算完毕*/


        System.out.println("你输入的两个日期间隔"+sum+"天！");

    }
}
